/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.layout.VerticalPanel;

import org.ops4j.gaderian.Messages;

/**
 * A dialog that lets the user create a password.
 * 
 * @version $Revision: 1.2 $
 * @author $Author: johan $
 */
public class CreatePasswordDialog extends JDialog
{

	private static final int MIN_PASSWORD_LENGTH = 6;
	private boolean closeOK = false;
	private Messages messages;
	private JPasswordField password;
	private JPasswordField verify;

	public CreatePasswordDialog(Workbench workbench, Messages messages,
			String titleKey, String messageKey) {
		super(workbench.getDialogOwner(), messages.getMessage(titleKey), true);
		setLocationRelativeTo(workbench.getDialogOwner());
		this.messages = messages;

		VerticalPanel mainLayout = new VerticalPanel();
		JLabel message = new JLabel(messages.getMessage(messageKey));
		JLabel passwordLabel = new JLabel(messages
				.getMessage("password.create.label"));
		JLabel verifyLabel = new JLabel(messages
				.getMessage("password.create.verify.label"));
		JButton okButton = new JButton(messages
				.getMessage("keystore.general.ok"));
		okButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e)
			{
				if (validatePassword()) {
					closeOK = true;
					dispose();
				}
			}
		});
		JButton cancelButton = new JButton(messages
				.getMessage("keystore.general.cancel"));
		cancelButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e)
			{
				closeOK = false;
				dispose();
			}
		});
		password = new JPasswordField(20);
		verify = new JPasswordField(20);

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		mainLayout.addRow(message);
		mainLayout.addRow(passwordLabel, password);
		mainLayout.addRow(verifyLabel, verify);
		mainLayout.addRow(buttonPanel);

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(mainLayout, BorderLayout.CENTER);
	}

	private boolean validatePassword()
	{
		char[] passwordData = password.getPassword();
		if ((passwordData == null)
				|| (passwordData.length < MIN_PASSWORD_LENGTH)) {
			complain(messages.format("password.create.validate.toshoort", ""
					+ MIN_PASSWORD_LENGTH));
			return false;
		}
		char[] verifyData = verify.getPassword();
		if ((verifyData == null) || (verifyData.length < MIN_PASSWORD_LENGTH)) {
			complain(messages.getMessage("password.create.validate.nomatch"));
			return false;
		}
		if (verifyData.length != passwordData.length) {
			complain(messages.getMessage("password.create.validate.nomatch"));
			return false;
		}
		for (int i = 0; i < verifyData.length; i++) {
			if (verifyData[i] != passwordData[i]) {
				complain(messages
						.getMessage("password.create.validate.nomatch"));
				return false;
			}
		}
		return true;
	}

	private void complain(String message)
	{
		JOptionPane.showMessageDialog(this, message, messages
				.getMessage("password.create.validate.title"),
				JOptionPane.ERROR_MESSAGE);
	}

	public boolean isCloseOK()
	{
		return closeOK;
	}

	public char[] getPassword()
	{
		return password.getPassword();
	}
}
